#define  _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"

void STInit(ST* ps)
{
	assert(ps);

	ps->_a = NULL;
	ps->_top = ps->_capacity = 0;
}

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->_a);
	ps->_a = NULL;
	ps->_capacity = ps->_top = 0;
}

void STPush(ST* ps, STDateType x)
{
	if (ps->_top == ps->_capacity)
	{
		int newcapacity = ps->_capacity == 0 ? 5 : 2 * ps->_capacity;
		STDateType* pn = (STDateType*)realloc(ps->_a, sizeof(STDateType) * newcapacity);
		if (!pn)
		{
			perror("realloc");
			exit(-1);
		}
		ps->_a = pn;
		ps->_capacity = newcapacity;
	}
	ps->_a[ps->_top++] = x;
}

void STPop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));

	ps->_top--;
}

STDateType STTop(ST* ps)
{
	assert(ps);
	assert(!STEmpty(ps));

	return ps->_a[ps->_top - 1];
}

int STSize(ST* ps)
{
	assert(ps);

	return ps->_top;
}

bool STEmpty(ST* ps)
{
	assert(ps);

	return ps->_top == 0;
}